﻿RootUrl:<input type="text" id="rootUrl" value="@ViewBag.RootUri" />
AppKey:<input type="text" id="appKey" value="@ViewBag.AppKey" />
Project:<input type="text" id="project" value="@ViewBag.Project" />
<button type="button" id="changeAppProjectInfo">更换</button>
<div>Insert Bucket</div>
<input type="text" id="bucket_name" />
<button id="checkBucket" type="button">CheckBucket</button>
<button id="insertBucket" type="button">InsertBucket</button>
<div>Delete Bucket</div>
<select id="bucketList"></select>
<button id="deleteBucket" type="button">DeleteBucket</button>
<hr />
<div>Insert Object</div>
<div id="filePicker" class="fileInput" data-multiple="true">
    <button type="button" class="fileInput_browse_button">
        浏览</button>
</div>
<div>Delete Object</div>
<select id="objectList"></select>
<button id="deleteObject" type="button">DeleteObject</button>
@section Script{
    <script type="text/javascript" language="javascript">
        var rootUri = "@ViewBag.RootUri",
            servicePath = "v1/",
            appKey = "@ViewBag.AppKey",
            project = "@ViewBag.Project";

        $("#changeAppProjectInfo").click(function () {
            rootUri = $("#rootUrl").val();
            appKey = $("#appKey").val();
            project = $("#project").val();
            listBuckets();
        });
        var bucketList = $("#bucketList");

        $("#checkBucket").click(function () {
            var bucketName = $("#bucket_name").val();
            if (bucketName == "") {
                alert("请输入Bucket名称");
                return;
            }
            var httpClient = new HttpClient();
            httpClient.getJson(rootUri + servicePath + "b/" + bucketName + "?key=" + appKey + "&project=" + project, { "name": $("#bucket_name").val() }).read().done(function (data) {
                if (data) {
                    alert(bucketName + "已存在！");
                } else {
                    alert(bucketName + "不存在，可以添加！");
                }
            });
        });

        $("#insertBucket").click(function () {
            var bucketName = $("#bucket_name").val();
            if (bucketName == "") {
                alert("请输入Bucket名称");
                return;
            }
            var httpClient = new HttpClient();
            httpClient.postJson(rootUri + servicePath + "b?key=" + appKey + "&project=" + project, { "name": bucketName }).read().done(function (data) {
                bucketList.append("<option>" + data.name + "</option>");
                bucketList.find("option:last").prop("selected", true);
            });
        });

        $("#deleteBucket").click(function () {
            var bucketName = bucketList.find("option:selected").text();
            if (bucketName == "") {
                return;
            }
            var httpRequest = new HttpRequest("delete", rootUri + servicePath + "b/" + bucketName + "?key=" + appKey + "&project=" + project);
            httpRequest.execute().read().done(function (data) {
                bucketList.find("option:selected").remove();
            });
        });

        var objectList = $("#objectList");

        function listBuckets() {
            bucketList.empty();
            var httpClient = new HttpClient();
            
            httpClient.getJson(rootUri + servicePath + "b?key=" + appKey + "&project=" + project).read().done(function (data) {
                data.items.each(function (item) {
                    bucketList.append("<option>" + item.name + "</option>");
                });
                bucketList.trigger("change");
            });
        }

        iui.ready(function () {
            listBuckets();

            Inovout.View.get("filePicker").selectedChanged.addListener(insertObject);
        });
        bucketList.change(function () {
            var bucketName = bucketList.find("option:selected").text();
            if (bucketName == "") {
                return;
            }
            objectList.empty();
            var httpClient = new HttpClient();
            httpClient.getJson(rootUri + servicePath + "b/" + bucketName + "/o?key=" + appKey).read().done(function (data) {
                data.items.each(function (item) {
                    objectList.append("<option>" + item.name + "</option>");
                });
            });

        });
        function insertObject(fi, files) {
            var bucketName = bucketList.find("option:selected").text();
            if (bucketName == "") {
                alert("必须选择一个Bucket");
                return;
            }
            files.each(function (file) {
                var httpClient = new HttpClient();
                httpClient.postFile(rootUri + "upload/v1/b/" + bucketName + "/o?key=" + appKey + "&project=" + project + "&name=" + file.name + "&uploadType=media", file).read().done(function (data) {
                    objectList.append("<option>" + data.name + "</option>");
                });
                objectList.find("option:last").prop("selected", true);
            });
        }

        $("#deleteObject").click(function () {
            var bucketName = bucketList.find("option:selected").text();
            if (bucketName == "") {
                alert("必须选择一个Bucket");
                return;
            }

            var objectName = objectList.find("option:selected").text();
            if (bucketName == "") {
                alert("必须选择一个Object");
                return;
            }
            var httpRequest = new HttpRequest("delete", rootUri + servicePath + "b/" + bucketName + "/o/" + objectName + "?key=" + appKey);
            httpRequest.execute().read().done(function (data) {
                objectList.find("option:selected").remove();
            });
        });

    </script>

}
